package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.HospitalSchedule;
import team.yidong.fenda.repository.HospitalScheduleRepository;
import team.yidong.fenda.service.HospitalScheduleService;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by xuanlong on 2018/3/30.
 */
@Service
public class HospitalScheduleServiceImpl implements HospitalScheduleService {
    @Autowired
    HospitalScheduleRepository hospitalScheduleRepository;

    @Override
    public JSONArray findAll(Integer hospitalId, String date) {
        List<HospitalSchedule> hospitalScheduleList = hospitalScheduleRepository.findAll(new Specification<HospitalSchedule>() {
            @Override
            public Predicate toPredicate(Root<HospitalSchedule> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (date != null && !date.equals("")) {
                    Predicate p = criteriaBuilder.equal(root.get("date").as(String.class), date);
                    predicateList.add(p);
                }
                Predicate p = criteriaBuilder.equal(root.get("hospitalId").as(Integer.class), hospitalId);
                predicateList.add(p);
                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        });
        return JSONArray.fromObject(hospitalScheduleList);
    }

    @Transactional
    @Override
    public String addSchedule(Integer hospitalId, Integer peopleNum, String dayTime, String msg1, String msgDate, String msg2) throws ParseException {
        HospitalSchedule hospitalSchedule = hospitalScheduleRepository.findSchedule(hospitalId, peopleNum);
        if (hospitalSchedule != null) {
            return "添加的记录已经存在，可通过修改按钮进行修改信息";
        } else {
            HospitalSchedule hospitalSchedule1 = new HospitalSchedule();

            hospitalSchedule1.setDayTime(dayTime);
            hospitalSchedule1.setPeopleNum(peopleNum);
            hospitalSchedule1.setMsg(msg1 + msgDate + msg2);
            hospitalSchedule1.setHospitalId(hospitalId);
            hospitalSchedule1.setStatus("on");

            hospitalScheduleRepository.save(hospitalSchedule1);
            return "添加成功";
        }
    }

    @Transactional
    @Override
    public void updateSchedule(Integer scheduleId, String msg1, String msgDate, String msg2) {
        HospitalSchedule hospitalSchedule = hospitalScheduleRepository.findOne(scheduleId);
        hospitalSchedule.setMsg(msg1+msgDate+msg2);
        hospitalScheduleRepository.save(hospitalSchedule);
    }

    @Override
    public String findScheduleStatus(Integer hospitalId) {
        List<HospitalSchedule> hospitalScheduleList = hospitalScheduleRepository.findByHospitalId(hospitalId);
        HospitalSchedule hospitalSchedule = hospitalScheduleList.get(0);
        return hospitalSchedule.getStatus();
    }

    @Transactional
    @Override
    public void updateScheduleStatus(Integer hospitalId, String status) {
        List<HospitalSchedule> hospitalScheduleList = hospitalScheduleRepository.findByHospitalId(hospitalId);
        for (HospitalSchedule hospitalSchedule : hospitalScheduleList) {
            hospitalSchedule.setStatus(status);
            hospitalScheduleRepository.save(hospitalSchedule);
        }
    }
}
